﻿using Abp.Domain.Repositories;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;

namespace MyCompanyName.AbpZeroTemplate.Prm.Admin
{
    public class PlanTplAppService : AbpZeroTemplateAppServiceBase, IPlanTplAppService
    {
        private IRepository<PlanTpl> _planTplRepository;
        private IRepository<Gantt.TaskInfo> _taskInfoRepository;

        public PlanTplAppService(IRepository<PlanTpl> planTplRepository, IRepository<Gantt.TaskInfo> taskInfoRepository)
        {
            _planTplRepository = planTplRepository;
            _taskInfoRepository = taskInfoRepository;
        }

        public List<Dto.GetPlanTplTreeOutPut> GetTree()
        {
            var all = _planTplRepository.GetAllList();
            List<Dto.GetPlanTplTreeOutPut> result = new List<Dto.GetPlanTplTreeOutPut>();
            foreach (var item in all.Where(x => x.ParentId == 0))
            {
                var parent = new Dto.GetPlanTplTreeOutPut()
                {
                    day = item.Day,
                    id = item.Id.ToString(),
                    leaf = false,
                    parentId = item.ParentId.ToString(),
                    text = item.Name,
                    children = new List<Dto.GetPlanTplTreeOutPut>()
                };
                foreach (var child in all.Where(x => x.ParentId == item.Id))
                {
                    var _child = new Dto.GetPlanTplTreeOutPut()
                    {
                        day = child.Day,
                        id = child.Id.ToString(),
                        leaf = true,
                        parentId = child.ParentId.ToString(),
                        text = child.Name,
                        children = null
                    };
                    parent.children.Add(_child);
                }
                result.Add(parent);
            }
            return result;
        }


        public List<Dto.GetPlanTplCheckTreeOutPut> GetCheckTree()
        {
            var all = _planTplRepository.GetAllList();
            List<Dto.GetPlanTplCheckTreeOutPut> result = new List<Dto.GetPlanTplCheckTreeOutPut>();
            foreach (var item in all.Where(x => x.ParentId == 0))
            {
                var parent = new Dto.GetPlanTplCheckTreeOutPut()
                {
                    day = item.Day,
                    @checked = false,
                    id = item.Id.ToString(),
                    leaf = false,
                    parentId = item.ParentId.ToString(),
                    text = item.Name,
                    children = new List<Dto.GetPlanTplCheckTreeOutPut>()
                };
                foreach (var child in all.Where(x => x.ParentId == item.Id))
                {
                    var _child = new Dto.GetPlanTplCheckTreeOutPut()
                    {
                        day = child.Day,
                        id = child.Id.ToString(),
                        leaf = true,
                        @checked = false,
                        parentId = child.ParentId.ToString(),
                        text = child.Name,
                        children = null
                    };
                    parent.children.Add(_child);
                }
                result.Add(parent);
            }
            return result;
        }


        public void SetTplTask()
        {
            string Plans = HttpContext.Current.Request.Form["Plans"];
            var ProjectId = int.Parse(HttpContext.Current.Request.Form["ProjectId"]);

            List<Dto.CreateTplTaskInPut> list = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Dto.CreateTplTaskInPut>>(Plans);

            Dictionary<int, int> dic = new Dictionary<int, int>();
            foreach (var item in list.OrderBy(x => x.Id))
            {
                var task = new Gantt.TaskInfo()
                {
                    Name = item.Name,
                    ProjectId = ProjectId,
                    CreateOn = DateTime.Now,
                    StartDate = DateTime.Now,
                    EndDate = DateTime.Now.AddDays(item.Day)
                };

                if (dic.ContainsKey(item.ParentId))
                {
                    task.parentIdRaw = dic[item.ParentId];
                }
                var taskId = _taskInfoRepository.InsertAndGetId(task);
                dic.Add(item.Id, taskId);
            }
        }


        public async Task Create(Dto.CreatePlanTplInPut input)
        {
            await _planTplRepository.InsertAndGetIdAsync(new PlanTpl()
             {
                 Day = input.Day,
                 Name = input.Name,
                 ParentId = 0
             });
        }


        public async Task CreateItem(Dto.CreatePlanTplItemInPut input)
        {
            List<string> details = input.Detail.Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries).ToList();
            foreach (var item in details)
            {
                await _planTplRepository.InsertAsync(new PlanTpl()
                {
                    Day = 7,
                    Name = item,
                    ParentId = input.PlanTplId
                });
            }
        }


        public async Task Delete(int id)
        {
            await _planTplRepository.DeleteAsync(id);
            await _planTplRepository.DeleteAsync(x => x.ParentId == id);
        }


        public async Task Edit(List<Dto.EditPlanTplInPut> input)
        {
            foreach (var item in input)
            {
                var tpl = await _planTplRepository.GetAsync(item.Id);
                tpl.Name = item.Name;
                tpl.Day = item.Day;
            }
        }
    }
}
